package vip.xiaonuo.issued.impl.member;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberfeatures.entity.MemberFeatures;
import vip.xiaonuo.facedoor.modular.memberfeatures.service.MemberFeaturesService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.remote.iservice.adapter.MemberReqAdapter;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.iservice.IMemberDealWithService;
import vip.xiaonuo.facedoor.rule.RuleConstant;

import javax.annotation.Resource;
import java.util.List;

@Service(RuleConstant.DW + RuleConstant.MEMBER_DEAL_WITH)
public class MqttMemberDealWithServiceImpl implements IMemberDealWithService {

    @Autowired
    private MemberReqAdapter memberReqAdapter;

    @Autowired
    private MemberDeviceService memberDeviceService;

    @Autowired
    private DeviceLogService deviceLogService;


    @Resource
    private MemberFeaturesService memberFeaturesService;


    @Override
    public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
        return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
    }

    @Override
    public void deleteException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        memberDevice.setFailReason(ExceptionUtils.getMessage(e));
        memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, DELETE);
    }

    @Override
    public void delete(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
        memberUser.setMemberType(1);
        memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
        memberReqAdapter.deletePerson(memberDevice, memberDevice.getDeviceId() + "");
    }


    @Override
    public void add(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
        memberUser.setMemberType(1);
        // 存储柜管理员
        if (memberDevice.getLockerAdmin()) {
            memberUser.setPersonType(3);
        }
        if (StringUtils.isNotBlank(memberDevice.getLockerData())) {
            memberUser.setLockerData(memberDevice.getLockerData());
        }
        // 设置特征值
        addMemberFeatures(memberUser, deviceBase);
        memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
        memberReqAdapter.addPerson(memberDevice.getDeviceId() + "", memberUser);
    }

    private void addMemberFeatures(MemberUser memberUser, DeviceBase deviceBase) {
        MemberFeatures memberFeatures = null;
        if (ObjectUtil.equal(deviceBase.getDeviceType(), "1")) {
            memberFeatures = memberFeaturesService.getOne(new LambdaQueryWrapper<MemberFeatures>().eq(MemberFeatures::getPlatform, DeviceCategory.SUB_TYPE_FACE).eq(MemberFeatures::getDwMemberUserId, memberUser.getId()));
        } else if (ObjectUtil.equal(deviceBase.getDeviceType(), "5")) {
            memberFeatures = memberFeaturesService.getOne(new LambdaQueryWrapper<MemberFeatures>().eq(MemberFeatures::getPlatform, DeviceCategory.SUB_TYPE_STORE).eq(MemberFeatures::getDwMemberUserId, memberUser.getId()));
        }
        if (memberFeatures != null) {
            memberUser.setProperValue(memberFeatures.getValue());
        } else {
            memberUser.setProperValue(null);
        }
    }


    @Override
    public void addException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        // 直接修改为失败
        memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
    }

    @Override
    public void updateException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        // 直接修改为失败
        memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void update(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        memberUser.setMemberType(1);
        // 存储柜管理员
        if (memberDevice.getLockerAdmin()) {
            memberUser.setPersonType(3);
        }
        if (StringUtils.isNotBlank(memberDevice.getLockerData())) {
            memberUser.setLockerData(memberDevice.getLockerData());
        }
        memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
        memberReqAdapter.updatePerson(memberDevice.getDeviceId() + "", memberUser);

    }
}
